import { constantRoutes, asyncRoutes } from '@/router'
import Layout from '@/layout'

/**
 * Use meta.role to determine if the current user has permission
 * @param roles
 * @param route
 */
function hasPermission(permissionss, route) {
  if (route.name) {
    return permissionss.includes(route.name)
  } else if (permissionss.length > 0 && route.children.length > 0 && permissionss.filter(item => route.children.map(x => x.name).indexOf(item) > -1).length > 0) {
    return true
  }
}

export function filterAsyncRoutes(routes, permissionss) {
  const res = []
  routes.forEach(route => {
    const tmp = { ...route }
    if (hasPermission(permissionss, tmp)) {
      if (tmp.children) { tmp.children = filterAsyncRoutes(tmp.children, permissionss) }
      res.push(tmp)
    }
  })

  return res
}


export function assemblyrouter(all) {
  var levelOnes = all.filter(item => item.parentid == null)
  var outs = []
  for (let index = 0; index < levelOnes.length; index++) {
    var currChild = all.filter(x => x.parentid == levelOnes[index].id)
    var current = {
      id: levelOnes[index].id,
      path: '/' + levelOnes[index].permissionscode,
      component: Layout,
    }

    if (currChild.length <= 0) {
      current['children'] = [
        {
          path: '/' + levelOnes[index].permissionscode + '/index',
          component: resolve => { require([`@/views/${levelOnes[index].component.substring(8)}.vue`], resolve)},
          name: levelOnes[index].permissionscode,
          meta: { title: levelOnes[index].permissionscode, icon: levelOnes[index].icon }
        }
      ]
    } else {
      current['redirect'] = '/' + levelOnes[index].permissionscode
      current['alwaysShow'] = levelOnes[index].alwaysshow
      current['meta'] = { title: levelOnes[index].permissionscode,icon: levelOnes[index].icon}
      current['children'] = []
      GetTreeChild(current, all, levelOnes[index].permissionscode)
    }
    outs.push(current)
  }

  return outs
}
export function GetTreeChild(current, all) {
  var currChild = all.filter(x => x.parentid == current.id)
  var childs = []
  for (let index = 0; index < currChild.length; index++) {
    var thisChild = all.filter(x => x.parentid == currChild[index].id)
    var thiscurrent = {
      id: currChild[index].id,
      path: '/' + currChild[index].permissionscode,
      meta: {
        title: currChild[index].permissionscode, icon: currChild[index].icon
      }
    }

    if (thisChild.length <= 0) {
       thiscurrent['component'] = resolve => { require([`@/views/${currChild[index].component.substring(8)}.vue`], resolve)}
       thiscurrent['name'] = currChild[index].permissionscode
     
    } else {
      thiscurrent['redirect'] = '/' + currChild[index].permissionscode
      thiscurrent['alwaysShow'] = currChild[index].alwaysshow,
      thiscurrent['children'] = []
      GetTreeChild(thiscurrent, all)
    }
    childs.push(thiscurrent)
  }

  current.children = childs
}

const state = {
  routes: [],
  addRoutes: []
}

const mutations = {
  SET_ROUTES: (state, routes) => {
    state.addRoutes = routes
    state.routes = constantRoutes.concat(routes)
  }
}

const actions = {
  generateRoutes({ commit }, res) {
    return new Promise(resolve => {
      let accessedRoutes = assemblyrouter(res.permissionsmenus)
      // asyncRoutes
      if (res.roles.includes('admin')) { accessedRoutes = accessedRoutes } else { accessedRoutes = filterAsyncRoutes(accessedRoutes, res.permissionss) }
      commit('SET_ROUTES', accessedRoutes)
      resolve(accessedRoutes)
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
